<template>
  <base-btn
    :title="title"
    @submit="toDeliver"
    @resetForm="resetForm"
    :type="type"
  >
    <el-form label-width="0px" :model="form" :rules="rules" ref="ruleForm">
      <el-form-item label="" prop="state_id">
        <el-select
          v-model="form.state_id"
          placeholder="请选择状态"
          class="full-width"
        >
          <el-option
            v-for="item in newStateList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="" prop="suggestion">
        <el-input
          v-model="form.suggestion"
          type="textarea"
          :autosize="{ minRows: 4, maxRows: 8 }"
          placeholder="请输入备注/处理意见"
          style="margin: 20px 0"
        >
        </el-input>
      </el-form-item>
    </el-form>
  </base-btn>
</template>
<script>
import api from "@/api/index";
import workManagement from "@/api/workManagement/index";
import BaseBtn from "@/components/workflows/baseBtn";
import qs from "qs";
export default {
  components: { BaseBtn },
  props: {
    workflow_id: {
      type: Number,
      default: 0
    },
    id: {
      type: Number,
      default: 0
    },
    title: {
      type: String,
      default: ""
    },
    type: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      visible: false,
      newStateList: [],
      form: { state_id: null, suggestion: "" },
      rules: {
        state_id: {
          required: true,
          message: "请选择状态",
          trigger: "change"
        }
      }
    };
  },
  created() {
    this.getState();
  },
  methods: {
    getState() {
      api
        .getNewState(
          this.workflow_id,
          qs.stringify({ per_page: 1000, page: 1 })
        )
        .then(res => {
            this.newStateList = res.data.value;
            //console.log("this.newStateList", this.newStateList);
        });
    },
    // 留言
    toDeliver(callback) {
      this.$refs["ruleForm"].validate(valid => {
        if (valid) {
          workManagement.toChangeState(this.id, this.form).then(res => {
            this.successCallBack(res);
            callback && callback();
          });
        } else {
          return false;
        }
      });
    },
    successCallBack(res) {
        this.msgSuccess(this.title + "成功");
      this.$emit("reload");
      this.visible = false;
    },
    resetForm() {
      this.$refs["ruleForm"].resetFields();
    }
  }
};
</script>
<style scoped>
.el-form-item {
  margin-bottom: 0px;
}
</style>
